# -*- coding: utf-8 -*-

import cv2
import numpy as np
import utils
import data1.config as config


def mapping_line_parser(line):
    filename, label_yzm = line.split(',')
    label_yzm = label_yzm[0:label_yzm.find('=')]
    return filename, label_yzm


def char2one_hot(c):
    op_mapping = {
        '+': 10,
        '-': 11,
        '*': 12
    }

    one_hot = np.zeros(config.label_size, np.uint8)
    if c in op_mapping:
        one_hot[op_mapping[c]] = 1
    else:
        one_hot[int(c)] = 1
    return one_hot


def one_hot2char(one_hot):
    op_mapping = {
        10: '+',
        11: '-',
        12: '*'
    }
    index = np.argmax(one_hot)
    if index < 10:
        return str(index)
    else:
        return op_mapping[index]


def preprocessing(gray_img):
    # 高斯模糊 9
    gaussian_blur = 9
    # 阈值 176
    threshold = 176
    # gray_img = load_image_by_bytes(image_bytes)
    blur_img = cv2.GaussianBlur(gray_img, (gaussian_blur, gaussian_blur), 0)
    _, threshImg = cv2.threshold(blur_img, threshold, 255, cv2.THRESH_BINARY)
    return utils.cut_image(threshImg, lambda x, y, w, h: w < 15 and h < 15, config.char_image_width, config.char_image_height)
